export default function tdPublic (param , callback ) {
  //同盾使用
  // let tdParams = {
  //           display: 'popup',
  //           container: '#td' + Math.floor(Math.random()*(1-10000) + 10000),//绑定id加id随机数
  //           width:'2.65rem',
  //           height:'1.2rem'
  //         };
  //  tdPublic( tdParams, function (ret) {
  //             thiz.yzm_token = ret.validateToken；//返回token
  //             thiz.blackBox = ret.blackBox;  //getInfo值
  //           });
  'use strict';
  if (!param || !callback || !(param instanceof Object) || !(typeof callback == 'function')) {
    console.log('参数格式错误');
    return false;
  }
  let fm;
  let tdJsClass = 'tdScript';
  function addScript() {
    window._fmOpt = {
      display: 'bind', //popup方式渲染验证码
      container: '', //验证码button渲染的目标DOM，自动展现方式下必需，详情见后文
      area: '.customForm', //对于custom模式，弹出窗口的加载容器，详情见后文
      partner: "renrenkj",
      appName: "renrenkj_h5",
      fmb: true,
      initialTime: new Date().getTime(),
      token: "renrenkj" + "-" + new Date().getTime() + "-" + Math.random().toString(16).substr(2),
      getinfo: function () {
        return "e3Y6ICIyLjUuMCIsIG9zOiAid2ViIiwgczogMTk5LCBlOiAianMgbm90IGRvd25sb2FkIn0=";
      },
    };
    fm = document.createElement('script');
    fm.type = 'text/javascript';
    fm.setAttribute('class', tdJsClass);
    fm.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'static.tongdun.net/captcha/main/tdc.js?ver=1.0&t=' + (new Date().getTime() / 600000).toFixed(0);
    let s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(fm, s);
    s = fm = null;
  }
  let id = param.container || ''; //id
  if (!id || !param.width || !param.height) {
    console.log('container / width / height 必传');
    return false;
  }
  let tdParams = {
    //popup/float/custom/bind
    display: param.display || 'bind',
    container: id,
    width: param.width || '100%',
    height: param.height || '100%'
  };
  let count = 0;//限制次数
  function initTd() {
    tdScript = null;
    let timeout = null;
    if (!window._fmOpt || !(window._fmOpt.newCaptcha)) {
      count++;
      if( count === 50){
        clearTimeout(timeout);
        timeout = null;
        count = null;
        return;
      }
      timeout = setTimeout(function() {
        console.log('重载同盾');
        initTd(); //回调，防止_fmOpt资源未加载完毕
        clearTimeout(timeout);
        timeout = null;
      }, 100);
      return;
    }
    let newTd = window._fmOpt.newCaptcha(tdParams);
    if( param.beforeValidate && param.beforeValidate instanceof Function){
      newTd.beforeValidate = function () {
        return param.beforeValidate();
      };
    }
    newTd.onSuccess = function(validateToken) {
      if (validateToken) {
        let result = {
          validateToken: validateToken,
          blackBox: window._fmOpt.getinfo(),
          // newTd : newTd,//返回_fmOpt
          reset : newTd.reset,//返回重载同盾方法
          // removeTd : function () { //删除页面同盾弹窗
          //   let tdEle = document.getElementsByClassName('td-outer-wrapper')[0];
          //   if( tdEle ) {
          //     tdEle.remove();//防止点开验证，直接点击返回其他页面，同盾弹窗还在
          //   }
          //   tdEle = null;
          // }
        };
        tdParams = null;
        id = id.slice(1, id.length);
        timeout = setTimeout(function(){
          callback(result);
          clearTimeout(timeout);
          timeout = null;
        },800)
      }
      else {
        newTd.reset();
      }
    };
    initTd = null;
  }
  let tdScript = document.getElementsByClassName(tdJsClass)[0];
  if (!tdScript) {
    //防止重载js
    addScript();
  }
  initTd();
};
